function fig = fm_plotfig()
% FM_PLOTFIG create GUI for plotting variables
%
% FM_PLOTFIG()
%
%Author:    Federico Milano
%Date:      11-Nov-2002
%Update:    27-Feb-2003
%Update:    11-Sep-2003
%Version:   1.0.2
%
%E-mail:    federico.milano@ucd.ie
%Web-site:  faraday1.ucd.ie/psat.html
%
% Copyright (C) 2002-2019 Federico Milano

global Fig Settings Theme Path Snapshot Bus

if ~Bus.n
  fm_disp('No loaded system',2)
  return
end
if ishandle(Fig.plot),
  figure(Fig.plot)
  fm_plot('initlist')
  fm_plot('initxlabel')
  fm_plot('listvars')
  return
end

if strcmp(Settings.platform,'MAC')
  dm = 0.008;
else
  dm = 0;
end

Settings.zoom = '';
colori2= [ 1.0000    1.0000    0.5000;
           1.0000    0.5000    1.0000;
           0.5000    1.0000    1.0000;
           0.5000    0.5000    1.0000;
           0.5000    1.0000    0.5000;
           1.0000    0.5000    0.5000;
           0.7000    0.7000    0.7000];

if Settings.hostver < 8.04
  tipiplot = {'Standard Colors'; 'Black and White'; 'Black Numbers'; ...
              'Black Symbols';   'Colors & Symbols'; 'Colors & Numbers'};
else
  tipiplot = {'Standard Colors'; 'Black and White'};
end

h0 = figure('Color',Theme.color01, ...
            'Units', 'normalized', ...
            'Colormap',[], ...
            'DeleteFcn','Fig.plot = -1;', ...
            'FileName','fm_plotfig', ...
            'MenuBar','none', ...
            'Name','PSAT Plot', ...
            'NumberTitle','off', ...
            'PaperPosition',[18 180 576 432], ...
            'PaperType','A4', ...
            'PaperUnits','points', ...
            'Position',sizefig(0.7,0.7), ...
            'Resize','on', ...
            'ToolBar','none');
if Settings.hostver >= 8.04
  %set(h0)
  set(h0, 'NextPlot', 'add')
end
Fig.plot = h0;
fm_set colormap

% Axes
% -------------------------------------------------------------------------

% axes for plots
h1 = axes('Parent',h0, ...
          'Box','on', ...
          'CameraUpVector',[0 1 0], ...
          'CameraUpVectorMode','manual', ...
          'Color',[1 1 1], ...
          'ColorOrder',Settings.color, ...
          'FontName',Theme.font01, ...
          'FontSize',12, ...
          'Position',[0.09 0.4050 0.4754 0.5000], ...
          'Tag','Axes1', ...
          'XColor',[0 0 0], ...
          'XLimMode','manual', ...
          'YColor',[0 0 0], ...
          'YLimMode','manual', ...
          'ZColor',[0 0 0]);
Hdl.axesplot = h1;
if Settings.hostver >= 8.04
  set(h1, 'NextPlot', 'replace')
end

if isunix, set(h1,'FontSize',10), end

% axes for logo
h1 = axes('Parent',h0, ...
          'Box','on', ...
          'CameraUpVector',[0 1 0], ...
          'CameraUpVectorMode','manual', ...
          'Color',Theme.color11, ...
          'ColorOrder',colori2, ...
          'HandleVisibility','on', ...
          'HitTest','off', ...
          'Layer','top', ...
          'Position',[0.09 0.0663 0.23 0.23], ...
          'Tag','Axes2', ...
          'XColor',Theme.color03, ...
          'XLim',[0.5 200.5], ...
          'XLimMode','manual', ...
          'XTickLabelMode','manual', ...
          'XTickMode','manual', ...
          'YColor',Theme.color03, ...
          'YDir','reverse', ...
          'YLim',[0.5 150.5], ...
          'YLimMode','manual', ...
          'YTickLabelMode','manual', ...
          'YTickMode','manual', ...
          'ZColor',[0 0 0]);
if Settings.hostver >= 8.04
  set(h1, 'NextPlot', 'add')
end
Hdl.axeslogo = h1;
if ispc, set(gca,'XColor',[126 157 185]/255,'YColor',[126 157 185]/255), end

set(h0,'Units','pixels')
figdim = get(h0,'Position');
set(h0,'Units','normalized')
xd = round(figdim(3)*0.23);
yd = round(figdim(4)*0.23);

fractalimg = ['alhambra',int2str(round(rand*8))];
main_logo = 0; %round(rand);
if main_logo
  a = fm_mat('main_logo');
else
  a = flipud(fliplr(imread([Path.images,fractalimg,'.jpg'],'jpg')));
end
try
  if Settings.hostver >= 7.04
    a = imresize(a,[yd xd],'bilinear');
  else
    a = imresize(a,[yd xd],'bilinear',11);
  end
catch
  % imresize is not available!!!
end

h2 = image('Parent',h1, ...
           'CData',a, ...
           'Tag','Axes2Image1', ...
           'XData',[1 200], ...
           'YData',[1 150]);

% PSAT name, version and date in the left-bottom picture
if main_logo
  color1 = [0 0 0];
  color2 = [0 0.503 0];
  if sum(Theme.color02) < 1
    color1 = [1 1 1];
    color2 = [0 1 0];
  end

  h1 = text(15,40,'PSAT');
  set(h1, ...
      'Color',color1, ...
      'FontSize',12, ...
      'FontWeight','bold', ...
      'FontName','Times')
  h1 = text(15,140,Settings.date);
  set(h1, ...
      'Color',color2, ...
      'FontSize', 9, ...
      'FontWeight','bold', ...
      'FontName','Times')
  h1 = text(15,120,['Version ',Settings.version]);
  set(h1, ...
      'Color',color2, ...
      'FontSize', 9, ...
      'FontWeight','bold', ...
      'FontName','Times')
end

% Menu File
h1 = uimenu('Parent',h0, ...
	    'Label','File', ...
	    'Tag','MenuFile');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot exporttext', ...
	    'Label','Export data as plain text', ...
	    'Tag','PushText', ...
	    'Accelerator','p');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot exportmtv', ...
	    'Label','Export data as MTV plot file', ...
	    'Tag','PushText', ...
	    'Accelerator','v');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot exportscript', ...
	    'Label','Export data as Matlab script', ...
	    'Tag','PushText', ...
	    'Accelerator','s');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot export', ...
	    'Label','Export data as Color EPS', ...
	    'Tag','PushEPS', ...
	    'Accelerator','e');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot export', ...
	    'Label','Export data as Metafile', ...
	    'Tag','PushMeta', ...
	    'Accelerator','m');
if isunix, set(h2,'Enable','off'), end
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot export', ...
	    'Label','Export data as Matlab Figure', ...
	    'Tag','PushFig', ...
	    'Accelerator','f');
h2 = uimenu('Parent',h1, ...
	    'Callback','close(gcf)', ...
	    'Label','Exit', ...
	    'Tag','NetSett', ...
	    'Accelerator','x', ...
	    'Separator','on');

% Menu Plot
h1 = uimenu('Parent',h0, ...
	    'Label','Plot', ...
	    'Tag','MenuPlot');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot plotvars', ...
	    'Label','Plot graph', ...
	    'Tag','PushPlot', ...
	    'Accelerator','z');

% Menu Options
h1 = uimenu('Parent',h0, ...
	    'Label','Options', ...
	    'Tag','MenuOptions');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot limits', ...
	    'Label','Plot voltage limits', ...
	    'Tag','PlotVLim', ...
	    'Accelerator','a');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot usedegrees', ...
	    'Label','Use degrees for angles', ...
	    'Tag','PlotAngles', ...
	    'Accelerator','g');
if Settings.usedegree
  set(h2,'Checked','on')
end
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot usehertzs', ...
	    'Label','Use Hz for rotor speeds', ...
	    'Tag','PlotHertz', ...
	    'Accelerator','g');
if Settings.usehertz
  set(h2,'Checked','on')
end
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot userelspeeds', ...
	    'Label','Use relative rotor speeds', ...
	    'Tag','PlotRelativeSpeeds', ...
	    'Accelerator','g');
if Settings.userelspeed
  set(h2,'Checked','on')
end
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot limits', ...
	    'Label','Normalize line flows', ...
	    'Tag','NormSij', ...
	    'Accelerator','n');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot lowestv', ...
	    'Label','Plot 3 lowest voltages', ...
	    'Tag','LowestV', ...
	    'Accelerator','1');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot highestv', ...
	    'Label','Plot 3 highest voltages', ...
	    'Tag','HighestV', ...
	    'Accelerator','2');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot highests', ...
	    'Label','Plot 3 highest flows', ...
	    'Tag','HighestS', ...
	    'Accelerator','3');


% Menu Legend
h1 = uimenu('Parent',h0, ...
	    'Label','Legend', ...
	    'Tag','MenuLegend');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot togglelegend', ...
	    'Label','Legend on/off', ...
	    'Tag','PushLegend', ...
	    'Accelerator','l');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot moveup', ...
	    'Label','Move up name', ...
	    'Tag','PushPlot', ...
	    'Separator', 'on', ...
	    'Accelerator','u');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot movedown', ...
	    'Label','Move down name', ...
	    'Tag','PushPlot', ...
	    'Accelerator','d');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_plot editvarname', ...
	    'Label','Edit legend name', ...
	    'Tag','PushPlot', ...
	    'Accelerator','t');

% Listboxes
% ----------------------------------------------------------------------
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color03, ...
               'Callback','fm_plot listvars', ...
               'CreateFcn','fm_plot initlist', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color10, ...
               'Max',20, ...
               'Position',[0.6047  0.5070  0.1624  0.4043], ...
               'Style','listbox', ...
               'Tag','Listbox1', ...
               'Value',[]);

string = ['if strcmp(get(Fig.plot,''SelectionType''),', ...
          '''open''), fm_plot(''editvarname''), end'];
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color03, ...
               'Callback',string, ...
               'CreateFcn','set(gcbo,''String'',[''[1] '',Varname.uvars{Varname.idx(1)}])', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color06, ...
               'Position',[0.80291  0.64679  0.15789  0.26453], ...
               'Style','listbox', ...
               'Tag','Listbox2', ...
               'Value',1);

% Push buttons for editing the plotted variables
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_arrowup'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot moveup', ...
               'Position',[0.80291      0.5948    0.052632    0.044343], ...
               'TooltipString','Move Variable Up', ...
               'Tag','Pushbutton13');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_arrowdown'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot movedown', ...
               'Position',[0.85554      0.5948    0.052632    0.044343], ...
               'TooltipString','Move Variable Down', ...
               'Tag','Pushbutton14');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_edittex'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot editvarname', ...
               'Position',[0.90817      0.5948    0.052632    0.044343], ...
               'TooltipString','Edit Variable Name', ...
               'Tag','Pushbutton15');


% Frame, graphical buttons and toggle buttons
% ----------------------------------------------------------------------
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'ForegroundColor',Theme.color03, ...
               'Position',[0.35-0.008     0.06422     0.23/1.3333*1.25+0.008     0.231], ...
               'Style','frame', ...
               'Tag','Frame2');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_legend'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot togglelegend', ...
               'Position',[0.5065  0.21422    0.07/1.3333  0.07], ...
               'TooltipString','Legend', ...
               'Style','togglebutton', ...
               'Tag','Checkbox2');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_grid'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot togglegrid', ...
               'Position',[0.5065  0.14422    0.07/1.3333  0.07], ...
               'TooltipString','Grid', ...
               'Style','togglebutton', ...
               'Tag','Checkbox1');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_text'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot textprops', ...
               'Position',[0.5065  0.07422    0.07/1.3333  0.07+dm], ...
               'TooltipString','Axes and Label Font', ...
               'Tag','Push1');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'CData',fm_mat('plot_zoomy'), ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot zoomy', ...
               'Position',[0.454  0.21422    0.07/1.3333  0.07], ...
               'TooltipString','Y Zoom', ...
               'Style','togglebutton', ...
               'Tag','Pushbutton4');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_axesy'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot axesy', ...
               'Position',[0.454     0.14422    0.07/1.3333  0.07], ...
               'TooltipString','Y axis', ...
               'Style','togglebutton', ...
               'Tag','Pushbutton5');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_axesc'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot axescolor', ...
               'Position',[0.454    0.07422    0.07/1.3333  0.07+dm], ...
               'TooltipString','Axes Color', ...
               'Tag','Pushbutton6');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_linep'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','if ~ishandle(Fig.line), fm_linelist; else, figure(Fig.line), end', ...
               'Position',[0.4015    0.07422    0.07/1.3333  0.07+dm], ...
               'TooltipString','Line Properties', ...
               'Tag','Pushbutton7');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_zoomx'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot zoomx', ...
               'Position',[0.4015     0.21422    0.07/1.3333  0.07], ...
               'TooltipString','X Zoom', ...
               'Style','togglebutton', ...
               'Tag','Pushbutton11');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_axesx'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot axesx', ...
               'Position',[0.4015     0.14422   0.07/1.3333  0.07], ...
               'TooltipString','X axis', ...
               'Style','togglebutton', ...
               'Tag','Pushbutton9');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'CData',fm_mat('plot_axesxy'), ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot axesxy', ...
               'ListboxTop',0, ...
               'Position',[0.349     0.14422    0.07/1.3333  0.07], ...
               'TooltipString','Axes', ...
               'Style','togglebutton', ...
               'Tag','Pushbutton10');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'CData',fm_mat('plot_axesp'), ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot axesprops', ...
               'ListboxTop',0, ...
               'Position',[0.349    0.07422    0.07/1.3333  0.07+dm], ...
               'TooltipString','Axes Properties', ...
               'Tag','Pushbutton8');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_zoomxy'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot zoomxy', ...
               'ListboxTop',0, ...
               'Position',[0.349     0.21422    0.07/1.3333  0.07], ...
               'TooltipString','Zoom', ...
               'Style','togglebutton', ...
               'Tag','Pushbutton12');

% Popup Menus and related static texts
% ----------------------------------------------------------------------

% Line Styles
h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color01, ...
    'HorizontalAlignment','left', ...
    'Position',[0.8018   0.5443   0.1579   0.0306], ...
    'String','Line Styles:', ...
    'Style','text', ...
    'Tag','StaticText1');
h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color04, ...
    'Callback','fm_plot plottypes', ...
    'ForegroundColor',Theme.color05, ...
    'Position',[0.8018   0.5122   0.1579   0.0321], ...
    'String',tipiplot, ...
    'Style','popupmenu', ...
    'Tag','PopupMenu1', ...
    'Value',1);

% Reference angles
h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color01, ...
    'HorizontalAlignment','left', ...
    'Position',[0.8018   0.4725   0.1568   0.0306], ...
    'String','Reference Angle:', ...
    'Style','text', ...
    'Tag','StaticText2');
h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color04, ...
    'Callback','fm_plot plotvars', ...
    'ForegroundColor',Theme.color05, ...
    'Position',[0.8018   0.4389   0.1579   0.0321], ...
    'CreateFcn','fm_plot setangles', ...
    'String',{'None'}, ...
    'Style','popupmenu', ...
    'Tag','PopupMenu2', ...
    'Value',1);

h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'Enable', 'inactive', ...
    'BackgroundColor',Theme.color03, ...
    'Callback','fm_plot setxlabel', ...
    'CreateFcn','fm_plot initxlabel', ...
    'ForegroundColor',Theme.color10, ...
    'Position',[0.6047  0.0627  0.1624  0.4], ...
    'Style','listbox', ...
    'Max', 100, ...
    'String','None', ...
    'Tag','PopupMenu3');

% Snapshot radio button
% -----------------------------------------------------------------------
h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color02, ...
    'Position',[0.8018   0.3750   0.1579   0.0306], ...
    'String','Plot Snapshots', ...
    'Style','radiobutton', ...
    'Tag','Radiobutton1', ...
    'Value',0);
if length(Snapshot) <= 1, set(h1,'Enable','off'), end

% Push buttons
% ----------------------------------------------------------------------
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color03, ...
               'Callback','fm_plot plotvars', ...
               'FontWeight','bold', ...
               'ForegroundColor',Theme.color09, ...
               'Position',[0.8018   0.2027+0.065  0.1579   0.06], ...
               'String','Plot', ...
               'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_txt'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot exporttext', ...
               'FontWeight','bold', ...
               'ForegroundColor',Theme.color09, ...
               'Position',[0.8018   0.1975    0.0525   0.07+dm], ...
               'TooltipString','Export plot as text file', ...
               'String','', ...
               'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_mtv'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot exportmtv', ...
               'FontWeight','bold', ...
               'ForegroundColor',Theme.color09, ...
               'Position',[0.8018+0.0526   0.1975    0.0525   0.07+dm], ...
               'TooltipString','Export file in MTV format', ...
               'String','', ...
               'Tag','PushMTV');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_gnuplot'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot exportscript', ...
               'FontWeight','bold', ...
               'ForegroundColor',Theme.color09, ...
               'Position',[0.8018+0.0526+0.0526   0.1975   0.0525   0.07+dm], ...
               'TooltipString','Export plot as Matlab script', ...
               'String','', ...
               'Tag','PushJPG');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_eps'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot export', ...
               'Position',[0.8018   0.1277   0.0525   0.07+dm], ...
               'TooltipString','Export plot as Color EPS', ...
               'Tag','PushEPS');
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_meta'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot export', ...
               'Position',[0.8018+0.0526   0.1277   0.0525   0.07+dm], ...
               'TooltipString','Export plot as Windows Metafile', ...
               'Tag','PushMeta');
if isunix, set(h1,'Enable','off'), end
h1 = uicontrol('Parent',h0, ...
               'CData',fm_mat('plot_matlab'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_plot export', ...
               'Position',[0.8018+0.0526+0.0526   0.1277   0.0525   0.07+dm], ...
               'TooltipString','Export plot as Matlab Figure', ...
               'Tag','PushFig');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','Fig.plot = -1; close(gcf);', ...
               'Position',[0.8018   0.0627   0.1579  0.06], ...
               'String','Close', ...
               'Tag','Pushbutton3');

if nargout > 0, fig = h0; end
